<template>
  <div>
    <el-form :model="form" :rules="rules" ref="formRef" style="margin-top: 5%" label-width="120px">
      <el-form-item label="联系方式" prop="tel">
        <el-input v-model="form.tel"></el-input>
      </el-form-item>
      <el-form-item label="投诉原因" prop="complaintTpye">
        <el-select v-model="form.complaintTpye" placeholder="选择您的投诉原因">
          <el-option label="客服态度" value="客服态度" />
          <el-option label="平台虚假信息" value="平台虚假信息" />
        </el-select>
      </el-form-item>
      <el-form-item label="投诉描述" prop="desc">
        <el-input type="textarea" v-model="form.desc"></el-input>
      </el-form-item>
      <el-form-item label="图片上传">
        <!-- <el-upload :http-request="uploadImageRequest" :before-upload="beforeUpload" :on-success="handleSuccess"
          :on-remove="handleRemove" action="">
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload> -->
        <uploadImageVue ref="upload" :limits="3"></uploadImageVue>

      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import moment from 'moment'
import { postRequestWithFormData } from '../../../api/axiosRequest.js'
import { uploadImageToNet } from '../../../api/imageApi.js'
import uploadImageVue from '../../../components/images/uploadImage.vue'

export default {
  components: {
    uploadImageVue
  },
  data() {
    return {
      // 表单元素
      form: {
        tel: '', //电话
        complaintTpye: '', // 投诉类型
        desc: '', // 投诉描述
        image: '', //投诉图片
      },
      // 表单规则
      rules: {
        // 电话
        tel: [
          { required: true, message: '请输入您的联系方式', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入正确的电话号码', trigger: 'blur' },
        ],
        // 投诉类型
        complaintTpye: [
          {
            required: true,
            message: '请选择你的投诉原因',
            trigger: 'change',
          },
        ],
        // 投诉描述
        desc: [
          { required: true, message: '请输入投诉描述', trigger: 'blur' },
          { min: 10, message: '至少输入十个字', trigger: 'change' },
        ],
      }
    }
  },
  methods: {
    // 1.提交表单
    submitForm() {
      this.$refs.formRef.validate(async (valid, fields) => {
        if (valid) {
          try {
            // 上传图片的逻辑
            // let imgPath = ''
            let formdata = new FormData()
            let temp = this.$refs.upload.getFileExposed();
            if (typeof (temp) !== 'undefined') {
              formdata.append('file', temp[0].raw);
            }
            const userIds = this.$store.state.userId

            console.log("userIds:" + userIds);
            const result = await uploadImageToNet(formdata);
            console.log(result);
            //15390197897
            console.log("result.data:" + result.data);
            if (result.status === 1) {
              this.form.image = result.data.url
            }
            console.log("this.form.image:" + this.form.image);

            const complaintsResult = await postRequestWithFormData("/complaints/addNewComplaint", {
              type: this.form.complaintTpye,
              telephone: this.form.tel,
              description: this.form.desc,
              userid: userIds,
              imgPath: this.form.image,
              date: moment().format("YYYY-MM-DD")
            });
            console.log(complaintsResult);
            if (complaintsResult.status === 200) {
              if (complaintsResult.data.status === 1) {
                ElMessage({
                  message: complaintsResult.data.msg,
                  type: 'success'
                })
                this.resetForm()
              } else {
                ElMessage({
                  message: '网络错误！',
                  type: 'error'
                })
              }
            }
          }catch(error) {
            console.log(error);
          }
        } else {
          console.log('非法的提交！', fields)
        }
      })
    },

    // 2.重置表单
    resetForm() {
      this.$refs.formRef.resetFields()
      this.$refs.upload.clearFile()
    },

    // 3.选中图片
    // ...其他方法...
    handleFiles(files) {
      // files 是一个包含选中文件的数组
      if (files.length > 0) {
        const file = files[0];
        // 这里是你上传文件到图片床的逻辑
        // 例如，你可以使用 fetch API 来发送文件到 API
        console.log('File to upload:', file);
        // 上传文件到图片床...
      }
    },
  },
  mounted() {
    // 初始化上传组件
  }
}
</script>